package org.example.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.erp.domain.DeviceType;
import org.example.erp.domain.MaintenanceProject;
import org.example.erp.service.impl.MaintenanceProjectServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class SpotInspectionController {
    @Autowired
    private MaintenanceProjectServiceImpl maintenanceProjectService;

    @DeleteMapping("/spotInspection/delete")
    public DataResult delete(@RequestBody List<String> projectNo) {
        if(projectNo.isEmpty()){
            maintenanceProjectService.remove(null);
        }else{
            maintenanceProjectService.removeByIds(projectNo);
        }
        return DataResult.success();
    }

    @GetMapping("/spotInspection/find")
    public DataResult findReversoContexts(@RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage,
                                          @RequestParam(value = "pageSize",defaultValue = "1") Integer pageSize,
                                          @RequestParam(required = false) String projectNo,@RequestParam(required = false) String projectName,
                                          @RequestParam(required = false) String projectType
    ){
        LambdaQueryWrapper<MaintenanceProject> d = new LambdaQueryWrapper<>();
        IPage<MaintenanceProject> page = new Page<>(currentPage,pageSize);
        if (projectNo != null && !projectNo.isEmpty()) {
            d.like(MaintenanceProject::getProjectNo, projectNo);
        }
        if (projectName != null && !projectName.isEmpty()) {
            d.like(MaintenanceProject::getProjectName, projectName);
        }
        if (projectType != null && !projectType.isEmpty()) {
            d.like(MaintenanceProject::getProjectType, projectType);
        }

        IPage<MaintenanceProject> pageResult = maintenanceProjectService.page(page, d);
        return DataResult.success(pageResult);
    }
    @PostMapping("/spotInspection/insert")
    public DataResult insertReversoContexts(@RequestBody MaintenanceProject m){
        maintenanceProjectService.save(m);
        return DataResult.success();
    }
    @PutMapping("/spotInspection/update")
    public DataResult updateReversoContexts(@RequestBody MaintenanceProject m){
        maintenanceProjectService.updateById(m);
        return DataResult.success();
    }
}
